﻿#region

using AutoMapper;
using DomainModel.Domain.Model;
using DomainModel.Domain.Model.MessageAssemblyModel;
using DomainModel.Domain.Model.MessageClipModel;
using DomainModel.Domain.Model.MessageTypeModel;
using DomainModel.Domain.Model.ServiceModel;
using DomainModel.Domain.Model.SubscriptionModel;
using NsbMonitorClientService.DataContract;

#endregion

namespace NsbMonitorClientService.Mapping
{
    public class AutoMapperInitialization
    {
        public void Init()
        {
            // Configure AutoMapper
            Mapper.CreateMap<Service, ServiceShortInfo>()
                .ForMember(dest => dest.Active, opt => opt.UseValue(true))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.GetFriendlyName()))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Summary))
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Publisher, opt => opt.MapFrom(src => src.IsPublisher()))
                .ForMember(dest => dest.Subscriber, opt => opt.MapFrom(src => src.IsSubscriber()))
                .ForMember(dest => dest.Handler, opt => opt.MapFrom(src => src.IsHandler()))
                .ForMember(dest => dest.LastStarted, opt => opt.MapFrom(src => src.LastStarted))
                .ForMember(dest => dest.LastSeen, opt => opt.MapFrom(src => src.LastAlive));

            Mapper.CreateMap<Service, ServiceDetails>()
                .ForMember(dest => dest.Active, opt => opt.UseValue(true))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.GetFriendlyName()))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Summary))
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Publisher, opt => opt.MapFrom(src => src.IsPublisher()))
                .ForMember(dest => dest.Subscriber, opt => opt.MapFrom(src => src.IsSubscriber()))
                .ForMember(dest => dest.Handler, opt => opt.MapFrom(src => src.IsHandler()))
                .ForMember(dest => dest.Handles, opt => opt.MapFrom(src => src.GetHandledMessageTypes()))
                .ForMember(dest => dest.FirstSeen, opt => opt.MapFrom(src => src.FirstSeen))
                .ForMember(dest => dest.LastAlive, opt => opt.MapFrom(src => src.LastAlive))
                .ForMember(dest => dest.InputQueue, opt => opt.MapFrom(src => src.InputQueue.Address))
                .ForMember(dest => dest.ErrorQueue, opt => opt.MapFrom(src => src.ErrorQueue != null ? src.ErrorQueue.Address : null))
                .ForMember(dest => dest.AuditQueue, opt => opt.MapFrom(src => src.AuditQueue != null ? src.AuditQueue.Address : null));

            Mapper.CreateMap<QueueInfo, QueueShortInfo>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Address))
                .ForMember(dest => dest.MessagesInQueue, opt => opt.MapFrom(src => src.MessagesCount))
                .ForMember(dest => dest.OldestMessageTime, opt => opt.MapFrom(src => src.OldestMessageTime));

            Mapper.CreateMap<MessageType, MessageTypeNameId>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));

           Mapper.CreateMap<MessageType, MessageTypeShortInfo>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.TypeDescription.TypeFullDescription.Description));

            Mapper.CreateMap<MessageAssembly, MessageAssemblyNameId>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));

            Mapper.CreateMap<MessageAssembly, MessageAssemblyDetails>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.FileName, opt => opt.MapFrom(src => src.FileName))
                .ForMember(dest => dest.FileSize, opt => opt.MapFrom(src => src.FileSize));

            Mapper.CreateMap<Service, ServiceNameId>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));           
                        
            Mapper.CreateMap<Subscription, SubscribedToShortInfo>()
                .ForMember(dest => dest.PublisherId, opt => opt.MapFrom(src => src.Publisher.Id.Id))
                .ForMember(dest => dest.PublisherName, opt => opt.MapFrom(src => src.Publisher.GetFriendlyName()))
                .ForMember(dest => dest.TypeId, opt => opt.MapFrom(src => src.MessageType.Id.Id))
                .ForMember(dest => dest.TypeName, opt => opt.MapFrom(src => src.MessageType.Name));          
            
            Mapper.CreateMap<Subscription, SubscribedByShortInfo>()
                .ForMember(dest => dest.SubscriberId, opt => opt.MapFrom(src => src.Subscriber.Id.Id))
                .ForMember(dest => dest.SubscriberName, opt => opt.MapFrom(src => src.Subscriber.GetFriendlyName()))
                .ForMember(dest => dest.TypeId, opt => opt.MapFrom(src => src.MessageType.Id.Id))
                .ForMember(dest => dest.TypeName, opt => opt.MapFrom(src => src.MessageType.Name));

            Mapper.CreateMap<MessageAssembly, MessageAssemblyShortInfo>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
                .ForMember(dest => dest.Hash, opt => opt.MapFrom(src => src.Hash))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));

            Mapper.CreateMap<MessageClip, MessageClipInfo>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.Id))
            .ForMember(dest => dest.Content, opt => opt.MapFrom(src => src.Content))
            .ForMember(dest => dest.Label, opt => opt.MapFrom(src => src.Label))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));
        }
    }
}